package com.alangyun.remind.repository.param;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("alangyu-setting-mapping")
public class SystemParameter implements Serializable {
	private static final String FILENAME = "institution-params.xml";
	private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

	@XStreamAlias("departments")
	private List<Department> departs;
	@XStreamAlias("work-places")
	private List<WorkPlace> workPlaces;

	public static SystemParameter create() {
		SystemParameter ret= new SystemParameter();
		ret.departs = new ArrayList<Department>();
		ret.workPlaces = new ArrayList<WorkPlace>();
		
		return ret;
	}
	public List<Department> getDeparts() {
		return departs;
	}

	public void setDeparts(List<Department> departs) {
		this.departs = departs;
	}

	public List<WorkPlace> getWorkPlaces() {
		return workPlaces;
	}

	public void setWorkPlaces(List<WorkPlace> workPlaces) {
		this.workPlaces = workPlaces;
	}

	public static SystemParameter loadFrom(String path) throws IOException {

		XStream xstream = new XStream();
		xstream.processAnnotations(SystemParameter.class);
		xstream.allowTypes(new Class[] { SystemParameter.class, 
				Department.class,
				WorkPlace.class
		});

		SystemParameter db = null;

		File file = new File(path, FILENAME);
		if (file.exists()) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
			db = (SystemParameter) xstream.fromXML(reader);

			reader.close();
		}

		if (db == null)
			db = SystemParameter.create();

		return db;
	}
	
	public void saveToFile(String path) throws IOException {

		// 然后保存数据库到文件
		XStream dbXStream = new XStream();
		dbXStream.processAnnotations(SystemParameter.class);
		BufferedWriter dbWriter = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(new File(path, FILENAME)), "utf-8"));
		dbWriter.write(XML + "\n" + dbXStream.toXML(this));
		dbWriter.close();
	}
	
	public static void main(String[] args) {
		SystemParameter ret = SystemParameter.create();
		Department unit= Department.create(101,"国网山西省电力公司太原供电公司本部");
		unit.getChildren().add(Department.create(10101, "办公室（党委办公室）"));
		unit.getChildren().add(Department.create(10102, "发展策划部"));
		unit.getChildren().add(Department.create(10103, "财务资产部（会计服务中心）"));
		unit.getChildren().add(Department.create(10104, "安全监察部（保卫部）"));
		unit.getChildren().add(Department.create(10105, "建设部"));
		unit.getChildren().add(Department.create(10106, "互联网办公室"));
		unit.getChildren().add(Department.create(10107, "集体企业管理办公室"));
		unit.getChildren().add(Department.create(10108, "办公室（党委办公室）"));		
		unit.getChildren().add(Department.create(10109, "审计部"));
		unit.getChildren().add(Department.create(10110, "党委组织部（人力资源部）"));
		unit.getChildren().add(Department.create(10111, "党委党建部（党委宣传部、工会、团委）"));
		unit.getChildren().add(Department.create(10112, "监察部（纪委办公室）"));
		unit.getChildren().add(Department.create(10113, "电力调度控制中心"));
		ret.getDeparts().add(unit);
		unit = Department.create(102, "运维检修部（检修公司）");
		unit.getChildren().add(Department.create(10204,"运维检修部（本部）"));
		unit.getChildren().add(Department.create(10205,"输电运检室"));
		unit.getChildren().add(Department.create(10206,"变电运维室"));
		unit.getChildren().add(Department.create(10207,"变电检修室"));
		unit.getChildren().add(Department.create(10208,"二次检修室"));
		unit.getChildren().add(Department.create(10209,"配电运检室"));
		unit.getChildren().add(Department.create(10210,"电缆运检室"));
		ret.getDeparts().add(unit);
		unit = Department.create(103, "营销部（农电工作部、客户服务中心）");
		unit.getChildren().add(Department.create(10301,"营销部（本部）"));
		unit.getChildren().add(Department.create(10302,"计量室"));
		unit.getChildren().add(Department.create(10303,"营业及电费室"));
		unit.getChildren().add(Department.create(10304,"市场及大客户服务室"));
		ret.getDeparts().add(unit);
		ret.getDeparts().add(Department.create(104, "物资部（物资供应中心）、经济技术研究所"));
		ret.getDeparts().add(Department.create(105, "信息通信公司（数据中心）、供电服务指挥中心（配网调控中心）"));
		ret.getDeparts().add(Department.create(106, "项目管理中心、综合服务中心"));
		unit = Department.create(107, "国网太原市小店区供电公司");
		unit.getChildren().add(Department.create(10701,"安监部"));
		unit.getChildren().add(Department.create(10702,"调控分中心"));
		unit.getChildren().add(Department.create(10703,"供电所"));
		unit.getChildren().add(Department.create(10704,"营销部"));
		unit.getChildren().add(Department.create(10705,"运检部"));
		unit.getChildren().add(Department.create(10706,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(108, "国网山西科创城供电公司");
		unit.getChildren().add(Department.create(10801,"安监部"));
		unit.getChildren().add(Department.create(10802,"调控分中心"));
		unit.getChildren().add(Department.create(10803,"供电所"));
		unit.getChildren().add(Department.create(10804,"营销部"));
		unit.getChildren().add(Department.create(10805,"运检部"));
		unit.getChildren().add(Department.create(10806,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(109, "国网太原市滨河供电公司");
		unit.getChildren().add(Department.create(10901,"安监部"));
		unit.getChildren().add(Department.create(10902,"调控分中心"));
		unit.getChildren().add(Department.create(10903,"供电所"));
		unit.getChildren().add(Department.create(10904,"营销部"));
		unit.getChildren().add(Department.create(10905,"运检部"));
		unit.getChildren().add(Department.create(10906,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(110, "国网太原市晋源区供电公司");
		unit.getChildren().add(Department.create(11001,"安监部"));
		unit.getChildren().add(Department.create(11002,"调控分中心"));
		unit.getChildren().add(Department.create(11003,"供电所"));
		unit.getChildren().add(Department.create(11004,"营销部"));
		unit.getChildren().add(Department.create(11005,"运检部"));
		unit.getChildren().add(Department.create(11006,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(111, "国网清徐县供电公司");
		unit.getChildren().add(Department.create(11101,"安监部"));
		unit.getChildren().add(Department.create(11102,"调控分中心"));
		unit.getChildren().add(Department.create(11103,"供电所"));
		unit.getChildren().add(Department.create(11104,"营销部"));
		unit.getChildren().add(Department.create(11105,"运检部"));
		unit.getChildren().add(Department.create(11106,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(112, "国网阳曲县供电公司");
		unit.getChildren().add(Department.create(11201,"安监部"));
		unit.getChildren().add(Department.create(11202,"调控分中心"));
		unit.getChildren().add(Department.create(11203,"供电所"));
		unit.getChildren().add(Department.create(11204,"营销部"));
		unit.getChildren().add(Department.create(11205,"运检部"));
		unit.getChildren().add(Department.create(11206,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(113, "国网古交市供电公司");
		unit.getChildren().add(Department.create(11301,"安监部"));
		unit.getChildren().add(Department.create(11302,"调控分中心"));
		unit.getChildren().add(Department.create(11303,"供电所"));
		unit.getChildren().add(Department.create(11304,"营销部"));
		unit.getChildren().add(Department.create(11305,"运检部"));
		unit.getChildren().add(Department.create(11306,"综合部"));
		ret.getDeparts().add(unit);
		unit = Department.create(114, "国网娄烦县供电公司");
		unit.getChildren().add(Department.create(11301,"安监部"));
		unit.getChildren().add(Department.create(11302,"调控分中心"));
		unit.getChildren().add(Department.create(11303,"供电所"));
		unit.getChildren().add(Department.create(11304,"营销部"));
		unit.getChildren().add(Department.create(11305,"运检部"));
		unit.getChildren().add(Department.create(11306,"综合部"));
		ret.getDeparts().add(unit);
		ret.getDeparts().add(Department.create(115, "山西明业电力工程有限公司"));
		ret.getDeparts().add(Department.create(116, "明力达设计公司"));



		
		ret.getWorkPlaces().add(WorkPlace.create(201, "经营类"));	
		ret.getWorkPlaces().add(WorkPlace.create(201, "管理类"));	
		ret.getWorkPlaces().add(WorkPlace.create(201, "技术类"));	
		ret.getWorkPlaces().add(WorkPlace.create(201, "技能类"));	
		ret.getWorkPlaces().add(WorkPlace.create(201, "服务类"));
		
		try {
			ret.saveToFile("d:\\");
			
			SystemParameter input = SystemParameter.loadFrom("d:\\");
			
			System.out.println("完成");
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
	}
}
